#include <QDebug>

#include "ModelView.h"

ModelView::ModelView(QWidget *parent, int type) : QFrame(parent), selectState(false)
{
    layout = new QGridLayout(this);
    viewType = new QComboBox(this);
    toolBar = new MVToolBar(this);
    //toolBar->addSeparator();
    glView = new GLDrawer(this);
    layout->setSpacing(0);
    layout->setMargin(0);
    viewType->setMinimumHeight(20);
    viewType->setMaximumHeight(20);
    viewType->addItem(QIcon(":/view-top.png"), "Top");
    viewType->addItem(QIcon(":/view-down.png"), "Down");
    viewType->addItem(QIcon(":/view-front.png"), "Front");
    viewType->addItem(QIcon(":/view-back.png"), "Back");
    viewType->addItem(QIcon(":/view-left.png"), "Left");
    viewType->addItem(QIcon(":/view-right.png"), "Right");
    viewType->addItem(QIcon(":/view-perspective.png"), "Perspective");
    viewType->addItem(QIcon(":/view-isometric.png"), "Isometric");
    glView->setViewMode((GLDrawer::ViewMode)viewType->currentIndex());
    QObject::connect(viewType, SIGNAL(currentIndexChanged(int)), SLOT(viewTypeChange(int)));
    viewType->setCurrentIndex(type);
    setStyleSheet("ModelView { border: 3px solid rgb(100, 100, 100); }");
    layout->addWidget(viewType, 0, 0, 1, 1);
    layout->addWidget(toolBar, 0, 1, 1, 1);
    layout->addWidget(glView, 1, 0, 1, 2);
    layout->setColumnStretch(1, 2);
    show();
}

ModelView::~ModelView()
{
    delete glView;
    delete layout;
}

void ModelView::mousePressEvent(QMouseEvent *event)
{
    select();
}

void ModelView::select()
{
    selectState = true;
    emit selected();
    setStyleSheet("ModelView { border: 3px solid rgb(73, 150, 186); border-radius: 4px; }");
}

void ModelView::unselect()
{
    selectState = false;
    setStyleSheet("ModelView { border: 3px solid rgb(100, 100, 100); }");
}

bool ModelView::isSelected()
{
    return selectState;
}

MVToolBar *ModelView::getToolBar()
{
    return toolBar;
}

void ModelView::viewTypeChange(int type)
{
    glView->setViewMode((GLDrawer::ViewMode)type);
}

GLDrawer *ModelView::getGLDrawer()
{
    return glView;
}
